// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    env: "yiliaojiufeng-6gt0kj1l1df8b7b7",
    traceUser: true,
})

// 云函数入口函数

const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    if (event.condition.id == '1') {
        return await db.collection('examples').where({
            time: db.RegExp({
                regexp: event.keyWord,
                options: 'i',
            })
        }).get()
    } else if (event.condition.id == '2') {
        return await db.collection('examples').where(_.or([{
            location1: db.RegExp({
                regexp: event.keyWord,
                options: 'i',
            })
        }, {
            location2: db.RegExp({
                regexp: event.keyWord,
                options: 'i',
            })
        }])).get()
    } else if (event.condition.id == '3') {
        return await db.collection('examples').where(_.or([{
            reason1: db.RegExp({
                regexp: event.keyWord,
                options: 'i',
            })
        }, {
            reason2: db.RegExp({
                regexp: event.keyWord,
                options: 'i',
            })
        }, {
            reason3: db.RegExp({
                regexp: event.keyWord,
                options: 'i',
            })
        }])).get()
    } else if(event.condition.id == '4') {
        return await db.collection('examples').where({
            pay: db.RegExp({
                regexp: event.keyWord,
                options: 'i',
            })
        }).get()
    } else if(event.condition.id == '5') {
        return await db.collection('examples').where({
            operation: db.RegExp({
                regexp: event.keyWord,
                options: 'i',
            })
        }).get()
    }
}